package springcloud.controller;

import cn.xjt.pojo.Dept;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import springcloud.service.DeptService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 内容：
 *
 * @author
 * @date 2021/2/27-16:57
 */

@RestController
public class DeptController extends BaseController{

	@Autowired
	DeptService deptService;

	@Autowired
	DiscoveryClient client;

	@RequestMapping(value = "/queryAll",method = RequestMethod.GET)
	public List<Dept> test(HttpServletRequest request, HttpServletResponse response) throws IOException {
//		response.setCharacterEncoding("utf-8");
//		response.setHeader("Content-Type", "text/html;charset=UTF-8");
//		PrintWriter writer = response.getWriter();
		List<Dept> depts = deptService.selectAll();
//		writer.print(depts);
		return depts;
	}

	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public String addDept(HttpServletRequest request, HttpServletResponse response,Dept dept) throws IOException {
//		response.setCharacterEncoding("utf-8");
//		response.setHeader("Content-Type", "text/html;charset=UTF-8");
//		PrintWriter writer = response.getWriter();
		boolean b = deptService.addDept(dept);
//		writer.print("添加成功");
		if (b){
			return "添加成功";
		}
		return "添加失败";
	}

	@RequestMapping(value = "/queryOne/{id}",method = RequestMethod.GET)
	public Dept get(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) throws IOException {
//		response.setCharacterEncoding("utf-8");
//		response.setHeader("Content-Type", "text/html;charset=UTF-8");
//		PrintWriter writer = response.getWriter();
		Dept dept = deptService.selectDept(id);
//		writer.print(dept);
		return dept;
	}

	/**
	 * 注册进来的微服务，获取微服务的信息
	 * @return
	 */
	@GetMapping("/getinfo")
	public Object get(){
		List<String> services = client.getServices();
		System.out.println("Discovery=>"+services);
		List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
		System.out.println(instances);
		return instances;
	}
}
